package com.example;

/**
 * @Author loubobooo
 * @Description #173. 二叉搜索树迭代器
 * @Date 2022/3/15
 */
public class BSTIterator {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private TreeNode list = null;

    public BSTIterator(TreeNode root) {
        parseTreeNode(root);
    }

    private void parseTreeNode(TreeNode treeNode){
        if(treeNode.right != null){
            parseTreeNode(treeNode.right);
        }
        treeNode.right = list;
        list = treeNode;

        if(treeNode.left != null){
            parseTreeNode(treeNode.left);
        }
    }

    public int next() {
        int val = list.val;
        list = list.right;

        return val;
    }

    public boolean hasNext() {
        return list != null;
    }


}
